package cn.lhl.flowable.task;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Encoder;

import javax.crypto.SecretKey;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.Base64;

/**
 * Created by hp on 2020/8/13.
 * 加解密工具类
 */
@Slf4j
public class EncDecUtil {
    /**
     * 生成数字签名
     */
    public static String generalSign(String appid,
                                     String appSecret,
                                     String secretKey,
                                     Long timestamp){
        try {
            String origin = String.format("%s-%s-%s-%s-%s",
                    appid,
                    appSecret,
                    timestamp + "",
                    secretKey,
                    appid);

            //进行Md5加密
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(origin.getBytes("UTF-8"));
            byte[] md = md5.digest();
            //通过BASE64生成数字签名
            return new String(new BASE64Encoder().encode(md));
        } catch (Exception e) {
            log.error("" + e);
            return null;
        }
    }

    /**
     * 校验数字签名
     */


    /**
     * 数据加密方法
     * */
    public static String enc(String data, String enckey) {
        String encryptedData = null;
        try {
            //字符串转byte秘钥
            byte[] decoded_key = Base64.getDecoder().decode(enckey);

            encryptedData = SecureUtil.aes(decoded_key).encryptHex(data);
        } catch (Exception e) {
            throw new RuntimeException("加密错误，错误信息：", e);
        }
        return encryptedData;
    }

    /**
     * 数据解密方法
     * */
    public static String dec(String cryptData, String enckey) {
        String decryptedData = null;
        try {
            //字符串转byte秘钥
            byte[] decoded_key = Base64.getDecoder().decode(enckey);
            decryptedData = SecureUtil.aes(decoded_key).decryptStr(cryptData);
        } catch (Exception e) {
            throw new RuntimeException("解密错误，错误信息：", e);
        }
        return decryptedData;
    }

    /**
     *  对实体进行加密操作，加密带有@EncDec注解的字段
     */

    /**
     *  对实体进行解密操作，解密带有@EncDec注解的字段
     */

    /**
     *  文件byte[]加密方法
     */
    public static byte[] encFile(byte[] file, String enckey){
        try {
            //字符串转byte秘钥
            byte[] decoded_key = Base64.getDecoder().decode(enckey);
            return SecureUtil.aes(decoded_key).encrypt(file);
        } catch (Exception e) {
            log.error("" + e);
            return null;
        }
    }

    /**
     *  文件byte[]解密方法
     */
    public static byte[] decFile(byte[] file, String enckey){
        try {
            //字符串转byte秘钥
            byte[] decoded_key = Base64.getDecoder().decode(enckey);
            return SecureUtil.aes(decoded_key).decrypt(file);
        } catch (Exception e) {
            log.error("" + e);
            return null;
        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
        String data="需要解解密的串";
        //获取对称秘钥
        SecretKey secretKey = SecureUtil.aes().getSecretKey();
        byte[] encoded = secretKey.getEncoded();
        //byte秘钥转字符串
        String encoded___key = Base64.getEncoder().encodeToString(encoded);
        System.out.println("Base64strKey:"+encoded___key);
        //字符串转byte秘钥
        byte[] decoded_key = Base64.getDecoder().decode(encoded___key);
        System.out.println("Base64_byteKey:" + decoded_key);

        System.out.println("密钥:"+encoded___key);


        String encString = SecureUtil.aes(decoded_key).encryptHex(data);
        System.out.println("encryptHex加密后:"+encString);

        String decryptStr = SecureUtil.aes(decoded_key).decryptStr(encString);

        System.out.println("decryptStr解密后:"+decryptStr);

        String enc = enc(data, encoded___key);
        System.out.println("enc:"+enc);

        String dec = dec(enc, encoded___key);
        System.out.println("dec:"+dec);

        //字符串转byte

        byte[] decode1 = StrUtil.bytes(data, CharsetUtil.CHARSET_UTF_8);
        System.out.println("StrUtil.bytes:"+decode1.length);

        byte[] encrypt = SecureUtil.aes(decoded_key).encrypt(decode1);
        //byte[] bytes = encFile(data_byte, encoded___key);
        System.out.println("encbyte[]:"+encrypt.length);

        //解密
        byte[] decrypt = SecureUtil.aes(decoded_key).decrypt(encrypt);
        //byte[] bytes1 = decFile(encrypt, encoded___key);
        System.out.println("decbyte[]:"+encrypt.length);
        String str = StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
        System.out.println("StrUtil.str:"+str);

        System.out.println("还原后:"+str);




        System.out.println("111PPPPPPPPPPPPPPPPPPPP");

        String enc1 = enc(data, encoded___key);
        System.out.println("enc1:::"+enc1);

        String dec1 = dec(enc1, encoded___key);
        System.out.println("dec1:::"+dec1);




        System.out.println("222PPPPPPPPPPPPPPPPPPPP");

        byte[] bytes = encFile(decode1, encoded___key);
        System.out.println("encFile:::"+bytes.length);

        byte[] bytesres = decFile(bytes, encoded___key);
        System.out.println("decFile:::"+bytesres.length);

        System.out.println("byte[]转回串::"+StrUtil.str(bytesres, CharsetUtil.CHARSET_UTF_8));

    }
}
